package com.huiyi.quickproj.jundui.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AutoDetectDelimiter {
    public static void main(String[] args) {
        try {
            String s = "107084520|A|REP|ABDELSALAM|MOHAMED|A|||N|15|||MALAGA CV||||ABSECON|NJ|08201|||||||||||||07/06/1954|10/02/1992|10700094|ABSECON|01|01||02/05/2008|PRESIDENTIAL PRIMARY|PRI|S|M|";
            int iii = s.indexOf("|");
            System.out.println("ou: " + iii);

            // 文件路径
            String filePath = "D:\\project\\ieducation_training\\外部项目\\jundui_csv2sql\\data\\china\\population_data.csv";

            // 读取文件的前几行（例如，前100行）
            int linesToRead = 100;
            String[] sampleLines = readSampleLines(filePath, linesToRead);

            // 尝试识别最佳分隔符
            String bestDelimiter = detectBestDelimiter(sampleLines);

            // 打印识别到的最佳分隔符
            System.out.println("Auto-detected delimiter: " + bestDelimiter);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 通过文件路径,和读取多少行数据
    public static String[] readSampleLines(String filePath, int linesToRead) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String[] sampleLines = new String[linesToRead];
        for (int i = 0; i < linesToRead; i++) {
            sampleLines[i] = reader.readLine();
        }
        reader.close();
        return sampleLines;
    }

    public static String detectBestDelimiter(String[] sampleLines) {
        // 尝试常见的分隔符
        String[] possibleDelimiters = {",", ":", "\t", ";", "|", ",,,", ":", " - "};
//        String[] possibleDelimiters = {","};
        Map<String, Integer> delimiterCounts = new HashMap<>();

        for (String delimiter : possibleDelimiters) {
            int count = countOccurrences(sampleLines, delimiter);
            delimiterCounts.put(delimiter, count);
        }

        Map.Entry<String, Integer> stringIntegerEntry = delimiterCounts.entrySet()
                .stream()
                .max(Map.Entry.comparingByValue())
                .orElse(null);

        // 选择出现次数最多的分隔符
        return stringIntegerEntry.getKey() + "#7#" + stringIntegerEntry.getValue();
    }

    public static int countOccurrences(String[] lines, String delimiter) {
        int max = 0;
        for(String line : lines){
            if(StringUtils.isEmpty(line)) continue;
            int c = countOccurrences(line, delimiter);
            if(c > max){
                max = c;
            }
        }

        return max;
    }

    private static int countOccurrences(String mainString, String subString) {
        int count = 0;
        int lastIndex = 0;

        while (lastIndex != -1) {
            lastIndex = mainString.indexOf(subString, lastIndex);

            if (lastIndex != -1) {
                count++;
                lastIndex += subString.length();
            }
        }

        return count;
    }

}
